package com.xuecheng.search.controller;

import com.xuecheng.api.search.EsCourseControllerApi;
import com.xuecheng.framework.domain.course.CoursePub;
import com.xuecheng.framework.domain.course.TeachPlanMediaPub;
import com.xuecheng.framework.domain.search.CourseSearchParam;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.search.service.EsCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhaoxiang
 * @version 1.0
 * @Date 2019/11/22 22:12
 */
@RestController
@RequestMapping("/search/course")
public class EsCourseController implements EsCourseControllerApi {

    @Autowired
    private EsCourseService esCourseService;

    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult<CoursePub> list(@PathVariable("page") int page,
                                               @PathVariable("size")int size,
                                               CourseSearchParam courseSearchParam) {
        return esCourseService.list(page, size, courseSearchParam);
    }

    @Override
    @GetMapping("/getall/{id}")
    public Map<String, CoursePub> getAll(@PathVariable("id") String id) {
        return esCourseService.getAll(id);
    }

    @Override
    @GetMapping(value="/getmedia/{teachPlanId}")
    public TeachPlanMediaPub getMedia(@PathVariable("teachPlanId") String teachPlanId) {
        //将课程计划id放在数组中
        String[] teachplanIds = new String[]{teachPlanId};
        //通过service查询ES获取课程媒资信息
        QueryResponseResult<TeachPlanMediaPub> queryResponseResult = esCourseService.getMedia(teachplanIds);
        QueryResult<TeachPlanMediaPub> queryResult = queryResponseResult.getQueryResult();
        if (queryResult != null && queryResult.getList() != null && queryResult.getList().size()>0){
            //返回对应的媒资信息
            return queryResult.getList().get(0);
        }
        return new TeachPlanMediaPub();
    }
}
